package com.service.zk;

import com.alibaba.fastjson2.JSON;
import com.bean.zk.Log;
import com.bean.zk.Power;
import com.bean.zk.Role;
import com.dao.zk.EmpDAO;
import com.dao.zk.LogDAO;
import com.dao.zk.PowerDAO;
import com.dao.zk.RoleDAO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class LogService {

    @Resource
    private LogDAO logDAO;
    @Resource
    private RoleDAO roleDAO;
    @Resource
    private EmpDAO empDAO;
    @Resource
    private PowerDAO powerDAO;
    @Resource
    private PowerService powerService;

    public List<Log> getAllLogs() {
        return logDAO.getAllLogs();
    }

    public int reback(String lid) {
        int effect = 0;
        Log log = logDAO.selLogByLid(Integer.parseInt(lid));

//        根据操作类型判断回退
        switch (log.getLtype()) {
//            更新
            case "update":
//        要进行操作的角色
                Role beforeRole = JSON.parseObject(log.getBefore(), Role.class);
                Role currole = roleDAO.geRoleByRid(beforeRole.getRid());
                if (currole != null) {
                    effect = roleDAO.updateRoleById(beforeRole.getRname(), beforeRole.getRemark(), beforeRole.getRid());
                }
                break;
//                管理
            case "manage":
                List<Integer> oldpower = toList(log.getBefore());
                List<Integer> newpower = toList(log.getAfter());

                //        获取rid
                String rid = getRid(log.getBefore());
                effect = powerService.updatePower(oldpower, newpower, rid);
                break;
//                插入
            case "insert":
                //        获取rid
                String rid1 = getRid(log.getAfter());
//                判断是否拥有权限，有则不能删除
                List<Power> power1 = powerDAO.getPowersById(Integer.parseInt(rid1));
//                判断该角色是否被绑定
                Map<String, Object> empByRid = empDAO.getEmpByRid(Integer.parseInt(rid1));
                if (power1.size() == 0 &&empByRid==null) {
                    effect = roleDAO.delRoleById(Integer.parseInt(rid1));
                }
                break;
//                删除
            case "delete":
                Role beforeRole2 = JSON.parseObject(log.getBefore(), Role.class);
                roleDAO.addRoleByRid(beforeRole2);
                break;
        }
        if (effect==1) {
//            如果回退成功删除该条日志
            logDAO.delete(Integer.parseInt(lid));
        }
        return effect;
    }

    public List<Integer> toList(String mapString) {
//        存储处理后的数据
        List<Integer> list = new ArrayList<>();
//        将json格式的字符串转为map
        Map map = JSON.parseObject(mapString, Map.class);
//        得到数组
        String idlist = map.get("power_id").toString();
        String[] s1 = idlist.substring(1, idlist.length() - 1).replace(" ", "").split(",");
        for (String s : s1) {
            if (s !=null&&s.length()!=0) {
                list.add(Integer.parseInt(s));
            }
        }
        return list;
    }

    public String getRid(String mapString) {
//        将json格式的字符串转为map
        Map map = JSON.parseObject(mapString, Map.class);
//        得到数组
        String rid = map.get("rid").toString();

        return rid;
    }
}
